var paginationTotal = 1;//记录总数
$(function(){
	$('[name=receiving_status]').val(1);	//	初始化状态为 1
	initData(0); //初始化分页数据
    $('#dateFrom,#dateTo').datepicker({
		changeMonth: true,
		changeYear: true,			 
		dateFormat: 'yy-mm-dd'
	});
	
	$("#printDialog").dialog({
		autoOpen:false,
		modal:true,
		width:500,
		show:"slide",
		buttons:{
			"OK":function(){
				$("#printForm").submit();
			},
			"Close":function(){
				$(this).dialog("close");
			}
		}
	});
	
	$("#tipDialog").dialog({
		autoOpen:false,
		modal:true,
		width:500,
		show:"slide",
		buttons:{
			"Close":function(){
				$(this).dialog("close");
			}
		}
	});
	
	$('.toggleButton').click(function(){
		if($(this).attr("refstatus") != 1){
			$(".detailInfo").hide();
			$(this).attr("refstatus",1);
            $(this).html($(this).attr("fold"));
		}else{
			$(".detailInfo").show();
			$(this).attr("refstatus",0);
            $(this).html($(this).attr("unfold"));
		}
	});
	
	$("#right_confirm").dialog({
		autoOpen:false,
		modal:true,
		height:160,
		show:"slide",
		buttons: {
			'OK': function() {
				loading();
				$.ajax({
					type: "post",
					async: false,
					dataType: "json",
					url: "/merchant/receiving/delete", 
					data: "id=" + Uid,
					success: function(json) { 
						if(json.ask=='1'){
							initData(0);							
							$("#tipDialog").html("Success.");
							
						}else{
							$("#tipDialog").html("Fail.");;
						}
					}
				});
				$(this).dialog('close');
//				$("#tipDialog").dialog("open");
			},
	    	'Cancel': function() {
					$(this).dialog('close');
			 }
		}

	});
	//回车处理
	$("#receiving_code").add("#ref_id").add("#dateFrom").add("#dateTo").keyup(function(e){
		var key = e.which;
		if(key==13){
			search();
		}
	});
});

function loadData(page,pageSize){
    var receiving_code = $('[name=receiving_code]').val();	//ASN No
    var ref_id = $('[name=ref_id]').val();				//Reference No
    var status = $('[name=receiving_status]').val();
    var dateFrom = $('[name=dateFrom]').val();
    var dateTo = $('[name=dateTo]').val();
	$.ajax({
		type: "POST",
		async: false,
		dataType: "json",
		url: "/merchant/receiving/list",
		data:{
			'page':page,'pageSize':pageSize,
            'receiving_code':receiving_code,'status':status,'dateFrom':dateFrom,'dateTo':dateTo,
            'ref_id':ref_id
		},
		success: function(json) {

			var headerHtml = '';		//不同状态不同的列表头
			if(status == 0 || status == 1){
				headerHtml += $("#awaiting-header-content").html();
			}
			if(status == 3 || status == 4){
				headerHtml += $("#received-header-content").html();
			}
			
			$(".row-header").html(headerHtml);
			var html = "";
			if(json.ask != 1) {
				paginationTotal = 0;
				html = '<div class="line1">&nbsp;No Data</div>';
			}else {
                paginationTotal = json.total;
                $.each(json.result,function(key,val){
					html += '<div class="row height25">';					
					if(status == 0 || status == 1 ){						
						html += '<div class="cell orderCode" style="width:17%" receivingId="'+val.receiving_id+'">'+val.receiving_code+'</div>';
						html += '<div class="cell " style="width:12%" title="'+val.ref_id+'">'+val.ref_id+'</div>';
						html += '<div class="cell " style="width:10%" title="'+val.order_type+'">'+val.order_type+'</div>';
						html += '<div class="cell " style="width:20%" title="'+val.created+'">'+val.created+'</div>';
						html += '<div class="cell " style="width:10%" title="'+val.totalQty+'">'+val.totalQty+'</div>';
						html += '<div class="cell " style="width:20%" title="'+val.description+'">'+val.description+'</div>';
						html += '<div class="cell no-background" style="width:11%;padding-top:4px;">';
							html += '<a href="/merchant/receiving/print-asnno/asn/'+val.receiving_code+'" target="_blank"><img src="/images/print-asnno.png" title="Print ASNNo" width="16" height="16" /></a>';	
							html += '<img src="/images/print-productlabel.png" title="Print Label" width="16" height="16" onclick="printLabel('+val.receiving_id+')" class="cursor" />';
							if(status == 1){
								html += '<a href="/merchant/receiving/edit/id/'+val.receiving_id+'"><img src="/images/icon_edit.gif" class="editIcon" /></a>';
								html += '<img class="cursor" onclick="receivingDel(\''+val.receiving_code+'\')" title="Delete" src="/images/icon_del.gif">';
							}							
						html += '</div>';					
						html += '</div>';
					}
					if(status == 3 || status == 4){
						html += '<div class="cell orderCode" style="width:20%" receivingId="'+val.receiving_id+'">'+val.receiving_code+'</div>';
						html += '<div class="cell " style="width:20%" title="'+val.ref_id+'">'+val.ref_id+'</div>';
						html += '<div class="cell " style="width:20%" title="'+val.order_type+'">'+val.order_type+'</div>';
						html += '<div class="cell " style="width:20%" title="'+val.totalQty+'">'+val.totalQty+'</div>';
						html += '<div class="cell no-background" style="width:20%" title="'+val.received_qty+'">'+val.received_qty+'</div>';					
						html += '</div>';
					}
					
					// 每次 的产品具体信息

                	html += '<div style="width:100%;border-bottom:1px solid #cccccc;display:none;font-style:italic;background-color:#CAD9EA;" id="detailInfo'+val.receiving_id+'" class="detailInfo">';
                    html += '<div class="row height25 bold">';
                    html +=$("#row-header-content").html();
					html += '</div>';


					$.each(val.products,function(pK,pV){
						html += '<div class="row height25">';
						html += '<div class="cell" style="width:13%"><a href="/merchant/product/view-product-info/productId/'+pV.product_id+'/edit/false" target="_blank">'+pV.SKUNo+'</a></div>';
						html += '<div class="cell" style="width:10%"><a href="/merchant/product/view-product-info/productId/'+pV.product_id+'/edit/false" target="_blank">'+pV.SKUName+'</a></div>';
						html += '<div class=" cell" style="width:10%">'+pV.weight+'</div>';
						html += '<div class=" cell" style="width:10%">'+pV.order_qty+'</div>';
						
						html += '<div class="cell" style="width:12%">'+pV.received_qty+'</div>';
						html += '<div class="cell no-background" style="width:18%">'+pV.remark+'</div>';
						html += '</div>';
						
					});
					html += '</div>';
                });
			}
			$("#receiving-list").html(html);	
			$(".orderCode").click(function(){
				$("#detailInfo"+$(this).attr("receivingId")).toggle();
			});
			$("#status0").html(json.statusTotal.deleteTotal);
			$("#status1").html(json.statusTotal.awaitingTotal);
			$("#status2").html(json.statusTotal.receivingTotal);
			$("#status3").html(json.statusTotal.receivedTotal);
			$("#status4").html(json.statusTotal.abnormalTotal);

		}	//success
	});
	
}

function search() {
   initData(0);
}

function printLabel(receiving_id) {
	
	$.ajax({
		type: "POST",
		async: false,
		dataType: "json",
		url: "/merchant/receiving/receiving-product",
		data:{
           	'receiving_id':receiving_id
		},
		success: function(json) {
			var html = '';
			if(json.ask != 1) {
				html = '<div class="line1">&nbsp;No data returns</div>';
			}else {
				html += "<form action='/merchant/product/print-label' method='post' id='printForm' target='_blank'>";
				html += '<div class="row height25 row-header">';
				html += '<div class="per20 cell" sortColumn="productName">SKU Name</div>';
				html += '<div class="per20 cell" sortColumn="sku">SKU</div>';
				html += '<div class="per20 cell " sortColumn="status">Status</div>';
				html += '<div class="per20 cell">Category</div>';
				html += '<div class="per15 cell no-background">Qty</div><div class="clearFloat"></div>';
				html += '</div>';
				html += '<div class="tab-div">';							
				$.each(json.result,function(k,v){
					html += '<div class="row height25">';
					html += '<div class="per20  cell" sortColumn="productName">'+v.product_title+'</div>';
					html += '<div class="per20 cell" sortColumn="sku">'+v.product_sku+'</div>';
					html += '<div class="per20 cell " sortColumn="status">'+v.product_status+'</div>';
					html += '<div class="per20 cell">'+v.category.poc_value+'</div>';
					html += '<div class="per15 cell no-background"><input type="text" class="width1" value="'+v.order_qty+'" name="count'+v.product_id+'"/></div>';
					html += '<div class="clearFloat"></div>';
					html += '</div>';
				});
				html += '</div>';
				html += "</form>";
				
			}
			$("#printDialog").html(html).dialog("open");
		}	//success
	});

}

//删除
function receivingDel(id){
	if(id==''||id==undefined){
		return false;
	}
	Uid = id;
	$('#right_confirm').dialog('open');
}

function clear(){
	$('[name=receiving_code]').val('');	//ASN No
    $('[name=ref_id]').val('');				//Reference No
    $('[name=receiving_status]').val('');
    $('[name=dateFrom]').val('');
    $('[name=dateTo]').val('');
}